{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# Day14 re中的split"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "　　在今天的日程里，我们将继续对`re`库中的实用API进行学习，今天要学习的是`re`中的`split`，不同与之前我们常用的`findall`的作用，`re.split`的作用是将传入的正则模式作为分割依据，从而对目标字符串进行分割，就像下面的例子一样："
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {
    "execution": {
     "iopub.execute_input": "2020-09-12T12:11:13.771208Z",
     "iopub.status.busy": "2020-09-12T12:11:13.771208Z",
     "iopub.status.idle": "2020-09-12T12:11:13.782143Z",
     "shell.execute_reply": "2020-09-12T12:11:13.781147Z",
     "shell.execute_reply.started": "2020-09-12T12:11:13.771208Z"
    }
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "['Python',\n",
       " 'is',\n",
       " 'an',\n",
       " 'interpreted',\n",
       " 'high-level',\n",
       " 'and',\n",
       " 'general-purpose',\n",
       " 'programming',\n",
       " 'language',\n",
       " '']"
      ]
     },
     "execution_count": 1,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "import re\n",
    "\n",
    "# 利用分割字符串的方式提取所有单词\n",
    "s = 'Python is an interpreted, high-level and general-purpose programming language.'\n",
    "\n",
    "re.split('[^a-zA-Z-]+', s)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "　　可以看到，对上面的例子当我们按照除了大小写字母和连字符号之外的其他任意单个及以上字符作为分割依据时，就可以把所有单词提取出来。\n",
    "\n",
    "　　如果用`()`包裹分割模式，那么**分割符**也会被单独作为分割出的字符片段被返回："
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {
    "execution": {
     "iopub.execute_input": "2020-09-12T12:11:13.785134Z",
     "iopub.status.busy": "2020-09-12T12:11:13.785134Z",
     "iopub.status.idle": "2020-09-12T12:11:13.801089Z",
     "shell.execute_reply": "2020-09-12T12:11:13.801089Z",
     "shell.execute_reply.started": "2020-09-12T12:11:13.785134Z"
    }
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "['Python',\n",
       " ' ',\n",
       " 'is',\n",
       " ' ',\n",
       " 'an',\n",
       " ' ',\n",
       " 'interpreted',\n",
       " ', ',\n",
       " 'high-level',\n",
       " ' ',\n",
       " 'and',\n",
       " ' ',\n",
       " 'general-purpose',\n",
       " ' ',\n",
       " 'programming',\n",
       " ' ',\n",
       " 'language',\n",
       " '.',\n",
       " '']"
      ]
     },
     "execution_count": 2,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "re.split('([^a-zA-Z-]+)', s)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "　　而`split`还有一个独特的参数：`maxsplit`，默认为0时不起作用，当设置为大于等于1的整数时，用于限定最大分割次数，当超过这个次数后，剩余未被分割的字符会作为返回结果的最后一个元素："
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {
    "execution": {
     "iopub.execute_input": "2020-09-12T12:11:13.803084Z",
     "iopub.status.busy": "2020-09-12T12:11:13.803084Z",
     "iopub.status.idle": "2020-09-12T12:11:13.819037Z",
     "shell.execute_reply": "2020-09-12T12:11:13.819037Z",
     "shell.execute_reply.started": "2020-09-12T12:11:13.803084Z"
    }
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "['Python',\n",
       " 'is',\n",
       " 'an',\n",
       " 'interpreted, high-level and general-purpose programming language.']"
      ]
     },
     "execution_count": 3,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "re.split('[^a-zA-Z-]+', s, maxsplit=3)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### Day14 课后小测验\n",
    "\n",
    "　　`split`在文本分句等场景中很常用，在学过今天的内容之后，请你换成`split`来完成**【Day13】**中的课后测验："
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {
    "execution": {
     "iopub.execute_input": "2020-09-12T12:11:13.820034Z",
     "iopub.status.busy": "2020-09-12T12:11:13.820034Z",
     "iopub.status.idle": "2020-09-12T12:11:13.832998Z",
     "shell.execute_reply": "2020-09-12T12:11:13.832998Z",
     "shell.execute_reply.started": "2020-09-12T12:11:13.820034Z"
    }
   },
   "outputs": [],
   "source": [
    "target = '''今年的最新动向，就是美国出于自身政治需要，公然放弃在主权争议上不持立场的\n",
    "承诺，直接介入本地区领土和海洋争端。仅上半年，美国就派出近3000架次军机、60余艘次军舰，\n",
    "包括多批次轰炸机和双航母编队，不断在南海炫耀武力，强化军事部署，甚至在与其毫不相干的争\n",
    "议海域横冲直撞，肆意推高地区冲突风险，正在成为南海军事化的最大推手。美国还对“南海行为\n",
    "准则”磋商指手划脚，干扰中国和东盟国家协商解决争议的努力，挑动地区国家对立对抗，美方的\n",
    "所做所为，就是要搞乱南海，搞乱地区，为推进其“印太战略”制造借口。'''"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "execution": {
     "iopub.execute_input": "2020-08-26T12:55:19.898241Z",
     "iopub.status.busy": "2020-08-26T12:55:19.897245Z",
     "iopub.status.idle": "2020-08-26T12:55:19.904225Z",
     "shell.execute_reply": "2020-08-26T12:55:19.903227Z",
     "shell.execute_reply.started": "2020-08-26T12:55:19.898241Z"
    }
   },
   "source": [
    "　　请将你的答案截图发到本帖评论区~"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "['今年的最新动向',\n",
       " '就是美国出于自身政治需要',\n",
       " '公然放弃在主权争议上不持立场的承诺',\n",
       " '直接介入本地区领土和海洋争端',\n",
       " '仅上半年',\n",
       " '美国就派出近3000架次军机、60余艘次军舰',\n",
       " '包括多批次轰炸机和双航母编队',\n",
       " '不断在南海炫耀武力',\n",
       " '强化军事部署',\n",
       " '甚至在与其毫不相干的争议海域横冲直撞',\n",
       " '肆意推高地区冲突风险',\n",
       " '正在成为南海军事化的最大推手',\n",
       " '美国还对“南海行为准则”磋商指手划脚',\n",
       " '干扰中国和东盟国家协商解决争议的努力',\n",
       " '挑动地区国家对立对抗',\n",
       " '美方的所做所为',\n",
       " '就是要搞乱南海',\n",
       " '搞乱地区',\n",
       " '为推进其“印太战略”制造借口']"
      ]
     },
     "execution_count": 9,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "[_.replace('\\n','') for _ in re.split('[^\\w“”、\\\\n]',target)[:-1]]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.6.1"
  },
  "widgets": {
   "application/vnd.jupyter.widget-state+json": {
    "state": {},
    "version_major": 2,
    "version_minor": 0
   }
  }
 },
 "nbformat": 4,
 "nbformat_minor": 4
}
